using System.Windows.Forms;
using ALib;

namespace TradingPlatform
{
    public partial class TradingStatus : UserControl
    {
        private const int _numberOfSubItems = 16;
        public TradingStatus()
        {
            InitializeComponent();

            for (int i = 0; i < _numberOfSubItems; i++)
            {
                string columnName = ((TradingStatusItemType) i).ToString();
                listView1.Columns.Add(columnName, columnName);
            }
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

        }

        public void BeginUpdate()
        {
            listView1.BeginUpdate();
        }

        public void EndUpdate()
        {
            listView1.EndUpdate();
        }

        public void UpdateValue(string tickerName, TradingStatusItemType tradingStatusItemType, string newValue)
        {
            if (!listView1.Items.ContainsKey(tickerName))
            {
                string[] subItems = new string[_numberOfSubItems];
                for (int i = 0; i < _numberOfSubItems; i++)
                {
                    subItems[i] = string.Empty;
                }
                ListViewItem tempListViewItem = listView1.Items.Add(new ListViewItem(subItems));
                tempListViewItem.Name = tickerName;

                // @"{0}@{1}@{2}@{3}@{4}", exchange, secType, symbol, expiry, currency
                string[] contractInfo = TickerNameAndId.ToContractInfo(tickerName);
                tempListViewItem.SubItems[(int)TradingStatusItemType.Exchange].Text = contractInfo[0];
                tempListViewItem.SubItems[(int)TradingStatusItemType.SecType].Text = contractInfo[1];
                tempListViewItem.SubItems[(int)TradingStatusItemType.Symbol].Text = contractInfo[2];
                tempListViewItem.SubItems[(int)TradingStatusItemType.Expiry].Text = contractInfo[3];
                tempListViewItem.SubItems[(int)TradingStatusItemType.Currency].Text = contractInfo[4];
            }

            ListViewItem[] listViewItemList = listView1.Items.Find(tickerName, false);
            GoodValue.Assert(listViewItemList.Length == 1);

            ListViewItem listViewItem = listViewItemList[0];

            listViewItem.SubItems[(int) tradingStatusItemType].Text = newValue;
        }
    }
}
